package com.complex.spring.advance.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class PerformanceTraceAspect {
	
	@Pointcut("execution(public void method1(String)) || execution(public void *.method2(int))")
	public void pointcutName(){}
	
	@Pointcut("within(com.complex.spring.advance.aop.aspectj.*)")
	public void pointcutwithin(){}
	
	@Pointcut("target(com.complex.spring.advance.aop.aspectj.Foo)")
	public void pointcutTarget(){}
	
	@Pointcut("this(com.complex.spring.advance.aop.aspectj.Foo)")
	public void pointcutThis(){}
	
	@Around("pointcutName()")
	public void performanceTrace(ProceedingJoinPoint joinpoint){
		System.out.println("performanceTrace execute");
		try {
			Object[] args = joinpoint.getArgs();
			for(Object arg:args){
				System.out.println("arg:"+arg.toString());
			}
			//必须调用proceed方法,目标对象才会被执行
			joinpoint.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Before("pointcutwithin()")
	public void beforeAdvice(JoinPoint joinpoint){
		System.out.println("beforeAdvice");
	}
	
	@After("pointcutTarget() && args(a)")
	public void afterAdvice(JoinPoint joinpoint,String a){
		System.out.println("afterAdvice:"+a);
	}
	
	@AfterReturning(pointcut="pointcutTarget()",returning="re")
	public void afterReturning(JoinPoint joinpoint,String re){
		System.out.println("afterReturning:"+re);
	}
	
	@AfterThrowing(pointcut="pointcutTarget()",throwing="e")
	public void afterThrowing(JoinPoint joinpoint,Exception e){
		System.out.println("afterThrowing:"+e);
	}
	
}
